package com.example.order.service.impl;

import com.example.order.entity.OrderEntity;
import com.example.order.entity.OrderStatus;
import com.example.order.repository.OrderRepository;
import com.example.order.service.RiderOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author : zhayh
 * @date : 2021-5-8 15:44
 * @description :
 */

@Slf4j
@Service
public class RiderOrderSerivceImpl implements RiderOrderService {
    @Resource
    private OrderRepository orderRepository;
    @Resource
    private OrderQueueService orderQueueService;

    @Override
    public OrderEntity confirmSend(String orderCode) {
        OrderEntity orderEntity = orderRepository.findByOrderCode(orderCode);
        if (orderEntity != null) {
            orderEntity.setOrderStatus(OrderStatus.DELIVING.getName());
            orderEntity.setOrderDeliverConfirmTime(LocalDateTime.now());
            orderEntity.setOrderDeliverName("金牌骑手");
            orderEntity.setOrderDeliverPhone("13901234567");
            orderEntity = orderRepository.save(orderEntity);
            log.info("骑手数据服务，骑手确认接单： {}", orderCode);

            return orderEntity;
        }
        return null;
    }

    @Override
    public OrderEntity finishDeliver(String orderCode) {
        OrderEntity orderEntity = orderRepository.findByOrderCode(orderCode);
        if (orderEntity != null) {
            orderEntity.setOrderStatus(OrderStatus.DELIVEFINISHED.getName());
            orderEntity.setOrderDeliverCompleteTime(LocalDateTime.now());
            orderEntity = orderRepository.save(orderEntity);
            log.info("骑手数据服务，骑手配送完成： {}", orderCode);

            // 向队列4发送配送完成消息
            orderQueueService.sendCompleteMessage(orderEntity.getOrderCode());
            return orderEntity;
        }
        return null;
    }
}
